<?php
namespace M\Action;
use Common\Model;
class PdditemAction extends BaseAction{
	public function _initialize() {
		parent::_initialize();
		$this->_mod = D('pdditems');
		if($this->getRobot()==false){
			$this->getrobot='no';
		}
	}
public function index(){
		$id = I('id', '','trim');
		$cach_name='pdd'.$id;
	$datainfo = S($cach_name);
	if(false === $datainfo || empty($datainfo)){
		$apiurl=$this->tqkapi.'/pdditem';
		$apidata=array(
    					'tqk_uid'=>$this->tqkuid,
    					'goods_id'=>''.$id.'',
    					'time'=>time()
    					);	
$token=$this->create_token(trim(C('yh_gongju')),$apidata);
$apidata['token']=$token;
$res= $this->_curl($apiurl,$apidata, true);
$ret = json_decode($res, true);	
$datainfo=$ret['result'];
if($ret['state']=='no' || empty($datainfo['goods_detail_response']['goods_details'])  || $datainfo['goods_detail_response']['goods_details'][0]['has_coupon']===false){
	
$this->delitem($id);
echo('<script>alert("此商品优惠券失效啦！");history.go(-1)</script>');		
}

//$datainfo['goods_detail_response']['goods_details'][0]['quanurl']=$retlink['result'];
$datainfo['goods_detail_response']['goods_details'][0]['goods_thumbnail_url']=str_replace("http://","https://",$datainfo['goods_detail_response']['goods_details'][0]['goods_thumbnail_url']);
$datainfo['goods_detail_response']['goods_details'][0]['goods_image_url']=str_replace("http://","https://",$datainfo['goods_detail_response']['goods_details'][0]['goods_image_url']);
$datainfo['goods_detail_response']['goods_details'][0]['goods_gallery_urls']=str_replace("http://","https://",$datainfo['goods_detail_response']['goods_details'][0]['goods_gallery_urls']);
S($cach_name,$datainfo,3600);
}
$datainfo=$datainfo['goods_detail_response']['goods_details'][0];
		$this->assign('mdomain',str_replace('/index.php/m','',C('yh_headerm_html')));
		$this->_config_seo(C('yh_seo_config.item'), array(
			'title' => $datainfo['goods_name'],
			'intro' => $datainfo['goods_desc'],
			'price' => $datainfo['min_group_price']/100,
			'quan' => floattostr($datainfo['coupon_discount']/100),
			'coupon_price' => $datainfo['min_group_price']/100-$datainfo['coupon_discount']/100,
			//'tags' => $tags,
			'seo_title' => $datainfo['goods_name'],
			'seo_keywords' => '',
			'seo_description' => $datainfo['goods_desc'],
			));

		$cid = $datainfo["category_id"];
		$where=array(
			'category_id'=>$cid,
			'goods_id'=>array('neq',$id)
			);
		$orlike = $this->_mod->where($where)->field('sold_quantity,goods_id,goods_name,goods_thumbnail_url,coupon_discount,min_group_price,min_normal_price')->limit('0,6')->order('tuisong desc,id desc')->select();
		$this->assign('orlike', $orlike);
		$this->assign('item', $datainfo);
		$buyer=$this->buyer(5);
		$this->assign('buyer', $buyer);
		$this->display();

	}
	protected function delitem($id){
	$where=array(
	'goods_id'=>$id,
	);
	$this->_mod->where($where)->delete();
	}
	
public function jumpout(){
$group=I('group','0','trim');
$id=I('id','0','trim');


$track=	trim(C('yh_youhun_secret'));

$apimap=array(
'tqk_uid'=>$this->tqkuid,
'goods_id_list'=>''.$id.'',
'pid'=>$track,
'time'=>time(),
'multi_group'=>$group
 );
$shorturl=$this->tqkapi.'/pddshortlink';
$sign=$this->create_token(trim(C('yh_gongju')),$apimap);
$apimap['token']=$sign;
$reslink= $this->_curl($shorturl,$apimap, true);
$retlink = json_decode($reslink, true);	
if($retlink['state']=='no'){
$this->delitem($id);
echo('<script>alert("获取领券链接失败，请稍后再试！");history.go(-1)</script>');		
}

if(!empty($retlink['result'])){
echo('<script>window.location.href="'.$retlink['result'].'"</script>');	
}
	
}
	
public function promurl(){
$apimap=array(
'tqk_uid'=>$this->tqkuid,
'pid'=>trim(C('yh_youhun_secret')),
'time'=>time()
 );
$shorturl=$this->tqkapi.'/pddhongbao';
$sign=$this->create_token(trim(C('yh_gongju')),$apimap);
$apimap['token']=$sign;
$reslink= $this->_curl($shorturl,$apimap, true);
$retlink = json_decode($reslink, true);	
if($retlink['state']=='no'){
$this->delitem($id);
echo('<script>alert("获取红包链接失败，请稍后再试！");history.go(-1)</script>');		
}

var_dump($retlink);

exit();
	
	
}
	
	
	
}

function floattostr( $val ){
	preg_match( "#^([\+\-]|)([0-9]*)(\.([0-9]*?)|)(0*)$#", trim($val), $o );
	return $o[1].sprintf('%d',$o[2]).($o[3]!='.'?$o[3]:'');
}	